package com.xmx.jd.task;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xmx.jd.pojo.Item;
import com.xmx.jd.service.ItemService;
import com.xmx.jd.util.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @Author Xumx
 * @Date 2021/3/17 15:28
 * @Version 1.0
 */
@Component
public class ItemTask {

    @Autowired
    private HttpUtils httpUtils;
    @Autowired
    private ItemService itemService;

    private static final ObjectMapper MAPPER = new ObjectMapper();
    int i = 1;
    @Scheduled(fixedDelay = 100*1000)   //当下载任务完成后，间隔多长时间进行下一次任务。
    public void itemTask() throws Exception{
        //声明需要解析的初始地址
        String url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&qrst=1&rt=1&stop=1&vt=2&wq%22%20+%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%22=%E6%89%8B%E6%9C%BA&cid2=653&cid3=655&s=113&click=0&page=";

        //按照页面对手机的搜索结果进行遍历解析
        for (int i = 0; i < 50; i=i+2) {
            String html = httpUtils.doGetHtml(url + i);
            //System.out.println(html);
            //解析页面，获取商品数据并存储
            this.parse(html);

        }

        System.out.println("手机数据抓取完成！"+i++);
    }


    //解析页面，获取商品数据并存储
    private void parse(String html) throws Exception {
        //解析html获取Document
        Document doc = Jsoup.parse(html);

        //获取spu信息
        Elements spuEles = doc.select("div#J_goodsList > ul > li");

        for (Element spuEle : spuEles) {
            //获取spu


            Elements skuEles = spuEle.select("div.gl-i-wrap");
            //System.out.println(skuEles);
            for (Element skuEle : skuEles) {

                Item item = new Item();

                Elements select = skuEle.select("div.p-img");
                //System.out.println(select);
                for (Element element : select) {
                    //获取商品的图片
                    String picUrl ="https:"+ element.select("img[data-lazy-img]").first().attr("data-lazy-img");
                    picUrl = picUrl.replace("/n9/","/n1/");
                    String picName = this.httpUtils.doGetImage(picUrl);
                    item.setPic(picName);

                    String attr = element.select("a").attr("title");
                    item.setTitle(attr);
                    long spu =Long.parseLong(element.select("div").attr("data-venid"));
                    //设置商品的spu
                    item.setSpu(spu);
                }
                Elements select1 = skuEle.select("div.p-operate > a");
                for (int i = 0; i < 1; i++) {
                    long sku = Long.parseLong(select1.get(1).attr("data-sku"));
                    item.setSku(sku);

                    //获取商品的详情的url
                    String itemUrl = "https://item.jd.com/" + sku + ".html";
                    item.setUrl(itemUrl);
                }
                /*for (Element element : select1) {

                }*/
                //根据sku查询商品数据


                List<Item> list = this.itemService.findAll(item);

                if(list.size()>0) {
                    //如果商品存在，就进行下一个循环，该商品不保存，因为已存在
                    continue;
                }



                //获取商品的价格
                Elements priEle = skuEle.select("div.p-price");
                //System.out.println(priEle);
                for (Element element : priEle) {
                    String pri = element.select("i").first().text();
                    //System.out.println(pri.length());
                    double price = Double.parseDouble(pri);
                    item.setPrice(price);
                }
                item.setCreated(new Date());
                item.setUpdated(item.getCreated());

                //保存商品数据到数据库中
                this.itemService.save(item);

            }
        }
    }
}
